package com.yale.elasticsearch.controller.base;

import com.yale.elasticsearch.dto.UserInfoDTO;
import com.yale.elasticsearch.service.base.DocumentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author :yale
 * @date : 2024/8/1
 */
@Api(tags = "基础操作-文档操作")
@RestController
@RequestMapping("/document")
public class DocumentController {

    @Resource
    DocumentService documentService;

    @ApiOperation(value = "获取文档", notes = "获取某个文档信息")
    @ApiParam(value = "文档ID", required = true)
    @GetMapping() //@PathVariable ("/{}")
    public Object getDocument(@ApiParam(value = "文档ID", required = true) @RequestParam("id") String id,
                              @ApiParam(value = "索引名称", required = true)@RequestParam("indexName") String indexName) {
        return documentService.getDocument(id,indexName);
    }

    @ApiOperation(value = "验证文档是否存在", notes = "验证某个文档是否存在")
    @GetMapping("/exist")
    public Object existDocument(@ApiParam(value = "文档ID", required = true)@RequestParam("id") String id,
                                @ApiParam(value = "索引名称", required = true)@RequestParam("indexName") String indexName) {
        return documentService.existsDocument(id,indexName);
    }

    @ApiOperation(value = "新增文档", notes = "新增一个文档")
    @PostMapping
    public Object addDocument(@RequestBody UserInfoDTO userInfoDTO) {
        return documentService.addDocument( userInfoDTO);
    }

    @ApiOperation(value = "更新文档", notes = "更新一个文档信息")
    @PutMapping
    public Object updateDocument(@RequestBody UserInfoDTO userInfoDTO) {
        return documentService.updateDocument(userInfoDTO);
    }

    @ApiOperation(value = "删除文档", notes = "删除一个文档信息")
    @DeleteMapping()
    public Object deleteDocument(@ApiParam(value = "文档ID", required = true) @RequestParam("id") String id,
                                 @ApiParam(value = "索引名称", required = true)@RequestParam("indexName") String indexName) {
        return documentService.deleteDocument(id,indexName);
    }

}